package mvv.learn.spring.dao;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import mvv.learn.spring.utitilies.AppUtilities;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

/**
 * @author Manh Vu
 */
public class DaoUtils {

	public static Criterion builSearchIn1000Supporter(String field, String key, Collection<?> list) {
		Criterion cr = null;

		Map<String, List<?>> map = AppUtilities.splitListIntoMulti(key, list);

		if(map != null) {
			Collection<List<?>> lists = map.values();
			Criterion[] predicates = new Criterion[lists.size()];
			int i = 0;
			for(List<?> l : lists) {
				predicates[i++] = Restrictions.in(field, l);
			}

			cr = Restrictions.or(predicates);
		}

		return cr;
	}
}
